
#include "nane/core/crt/Win32/Win32FileBase_.h"
#include "nane/core/assert.h"
#include <cassert>
#include <cstring>

namespace nane
{
    //////////////////////////////////////////////////////////////////////////
    Win32FileBase::Win32FileBase( FileSystem::Implementation* _creator, HANDLE _interface, const char* _filename )
        : SubSystemProductImplBase<FileSystem::Implementation, HANDLE>( _creator, _interface )
        , m_filename( NULL )
    {
        NANE_ASSERT( interface != INVALID_HANDLE_VALUE, "file handle must be valid" );
        NANE_ASSERT( _filename != NULL, "filename must be valid" );
        m_filename = new char[std::strlen(_filename) + 1];
        std::strcpy(m_filename, _filename);
    }
    //////////////////////////////////////////////////////////////////////////
    Win32FileBase::~Win32FileBase()
    {
        delete[] m_filename;
        m_filename = NULL;
    }
    //////////////////////////////////////////////////////////////////////////
    void Win32FileBase::Seek( uint32 _pos )
    {
        NANE_ASSERT_R( interface != INVALID_HANDLE_VALUE, "file handle must be valid" );
        ::SetFilePointer(interface, static_cast<LONG>(_pos), NULL, FILE_BEGIN);
    }
    //////////////////////////////////////////////////////////////////////////
    uint32 Win32FileBase::Tell()
    {
        NANE_ASSERT_RV( interface != INVALID_HANDLE_VALUE, "file handle must be valid", 0 );
        return static_cast<uint32>(::SetFilePointer(interface, 0, NULL, FILE_CURRENT));
    }
    //////////////////////////////////////////////////////////////////////////
    uint32 Win32FileBase::Size() const
    {
        NANE_ASSERT_RV( interface != INVALID_HANDLE_VALUE, "file handle must be valid", 0 );
        return static_cast<uint32>(::GetFileSize(interface, NULL));
    }
    //////////////////////////////////////////////////////////////////////////
}   // namespace nane
